package com.example.weather;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import static android.view.View.inflate;

public class ChoiceCity extends Activity
{
	MyCitiesFile myCitiesFile;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cities_layout);
		this.myCitiesFile=new MyCitiesFile(getApplicationContext());//一定要有这句，这句主要是给 MyCitiesFile 里的静态对象赋初值

		ListView listview=findViewById(R.id.Listview1);
		MyListViewAdapter myListViewAdapter=new MyListViewAdapter(getApplicationContext());
		listview.setAdapter(myListViewAdapter);
		listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				//Log.e("TAG", "onItemClick 被点击了 : "+ position);
				Intent intent=new Intent();
				intent.putExtra("city_name",MyCitiesFile.MyCitieslist.get(position).name);
				intent.putExtra("city_number",MyCitiesFile.MyCitieslist.get(position).number);
				MyCitiesFile.MyCitieslist.add(0,MyCitiesFile.MyCitieslist.get(position));
				myCitiesFile.save_cities();
				//Log.e("TAG", "onItemClick  1: ");
				setResult(3,intent);
				finish();
				//Log.e("TAG", "onItemClick  3: ");
				//在这里插入选中城市之后返回的代码
				//为了统一，这里返回城市在 MyCitiesFile.MyCitieslist 数组中的索引。
				//将内容返还到上个界面。intnet
			}
		});
		Log.e("TAG", "onCreate 1: "+MyCitiesFile.MyCitieslist.size());

		/*
		Log.e("TAG", "onCreate 2: ");
		MyCitiesFile.MyCitieslist.add(new MyCity("hehe","1234"));//添加一个新城市到动态数组
		Log.e("TAG", "onCreate 3: ");
		myCitiesFile.save_cities();//保存城市到文件
		Log.e("TAG", "onCreate 4: ");


		将最后一次选择的城市使用add(1,对象)，插入到第一个位置然后保存，之后再杀掉该页面
		 */

	}


	public void button_add_city(View view)
	{
		Intent intent;
		//Log.i("TAG", "button_choice_city 1:");
		intent = new Intent(ChoiceCity.this, AddCity.class);
		intent.putExtra("req_id",(int)0);
		//请求id为0表示为添加城市按键进行起调
		//请求id为其他表示为下个页面的城市分级的起调，见下个页面的注释
		//Log.i("TAG", "button_choice_city 2:");
		startActivityForResult(intent, 0);//打开添加城市页面
		//Log.i("TAG", "button_choice_city 3:");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		//super.onActivityResult(requestCode, resultCode, data);
		Log.e("TAG", "onActivityResult: ChoiceCity");
		if(resultCode==3)
		{
			//请求成功，拿到请求连接
			MyCitiesFile myCitiesFile=new MyCitiesFile(getApplicationContext());
			MyCitiesFile.MyCitieslist.add(0,new MyCity(data.getStringExtra("city_name"),data.getStringExtra("city_number")));
			myCitiesFile.save_cities();
			setResult(3,data);
			Toast.makeText(getApplicationContext(),"已添加  "+data.getStringExtra("city_name"),Toast.LENGTH_LONG).show();
			finish();
		}
	}

	class MyListViewAdapter extends BaseAdapter
	{
		Context context;
		MyListViewAdapter(Context context)
		{
			this.context=context;
		}

		@Override
		public int getCount()
		{
			return MyCitiesFile.MyCitieslist.size();
		}

		@Override
		public Object getItem(int position)
		{
			return position;
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View view =inflate(this.context, R.layout.list_item_text, null);
			TextView textview=view.findViewById(R.id.my_city_text);
			textview.setText(MyCitiesFile.MyCitieslist.get(position).name);
			return view;
		}

	}

}
